package com.gravata.enigma.repositorio.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Context;

import com.gravata.enigma.model.Bouquet;
import com.gravata.enigma.model.BouquetService;
import com.gravata.enigma.model.Profile;
import com.gravata.enigma.model.Service;
import com.gravata.enigma.repositorio.RepositorioBouquetService;
import com.gravata.enigma.repositorio.RepositorioGenerico;
import com.j256.ormlite.stmt.QueryBuilder;

public class RepositorioBouquetServiceImpl extends RepositorioGenerico<BouquetService>
		implements RepositorioBouquetService {

	public RepositorioBouquetServiceImpl(Context context) throws SQLException {
		super(context);
	}

	@Override
	public List<BouquetService> listar(Profile profile) {
		/*select b.profile, b.nome
			from bouquetservice bs
			inner join bouquet b
		where b.profile=profile*/

		try {
			QueryBuilder<Bouquet, ?> bQ=getDao(Bouquet.class).queryBuilder();
			QueryBuilder<BouquetService, ?> bsQ=getDatabase().queryBuilder();

			bQ.where().eq("profile_id", profile);

			bsQ.join(bQ);
			return bsQ.query();

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return Collections.emptyList();

	}

	@Override
	public List<Bouquet> listarBouquets(Service service) {

		List<BouquetService> bs= listarPorAtributo(BouquetService.SERVICE_ID_FIELD_NAME, service);

		List<Bouquet> bouquets=new ArrayList<Bouquet>();

		for(BouquetService b:bs){
			bouquets.add(b.getBouquet());
		}

		return bouquets;


	}

}